//
//  LAMerchantPhotoTableViewCell.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/11/2.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAMerchantPhotoTableViewCell.h"
#import "LZRestaurantPhotoAlbumModel.h"
#import "LZRestaurantPhotoModel.h"
#import "UIImageView+WebCache.h"
#import "MJPhotoBrowser.h"
#import "Masonry.h"

@interface LAMerchantPhotoTableViewCell ()

@property (weak, nonatomic) IBOutlet UIView *photoBGView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoBGViewHeightConstraint;

@property (strong, nonatomic) NSArray *arrModels;

@end

@implementation LAMerchantPhotoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(LZRestaurantPhotoAlbumModel *)model {
    _model = model;
    if (model) {
        [self setImgUrls:model.photos];
    }
    else {
        [self setImgUrls:@[]];
    }
}

-(void)setImgUrls:(NSArray *)imgUrls {
    for (UIView *aView in self.photoBGView.subviews) {
        [aView removeFromSuperview];
    }
    [self createSubImageviews:imgUrls];
}

-(void)createSubImageviews:(NSArray*)images {
    if (images.count == 0) {
        self.photoBGViewHeightConstraint.constant = 0;
        return;
    }
    
    self.arrModels = images;
    
    int rowNum = 4; // 每行的图片的最大数

    float width = 81*CGRectGetWidth([UIScreen mainScreen].bounds)/375.0;
    
    float padding = (CGRectGetWidth([UIScreen mainScreen].bounds)-width*rowNum-10*2)/3.0;
    
    
    UIImageView *lastView = nil;
    
    int row = 0;    // 行数
    int rank = 0;    // 列数
    
    for (int i = 0; i<images.count; i++) {
        LZRestaurantPhotoModel *model = images[i];
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.backgroundColor = [UIColor purpleColor];
        imgView.userInteractionEnabled = YES;
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.clipsToBounds = YES;
        [imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,model.photoUrl]] placeholderImage:[UIImage imageNamed:@"lz_other_placehoiderImage"]];
        imgView.tag = i;
        [self.photoBGView addSubview:imgView];
        
        [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTap:)]];
        
        lastView = imgView;
        
        row = i/rowNum+1;    // 行数
        rank = (i-(row-1)*rowNum)+1;    // 列数
        
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.and.height.mas_equalTo(width);
            if (row == 1) {
                make.top.equalTo(self.photoBGView.mas_top);
            }
            else {
                make.top.equalTo(self.photoBGView.mas_top).offset((width+padding)*(row-1));
            }
            if (rank == 1) {
                make.leading.equalTo(@0);
            }
            else {
                make.leading.equalTo(self.photoBGView.mas_leading).offset((width+padding)*(rank-1));
            }
        }];
    }
    
    CGFloat reviewImageHeight = row*width+(row-1)*padding;
    
    self.photoBGViewHeightConstraint.constant = reviewImageHeight;
}

//点击预览图片
- (void)photoTap:(UITapGestureRecognizer *)recognizer
{
    //1.创建图片浏览器
    MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init];
    
    //2.告诉图片浏览器显示所有的图片
    NSMutableArray *photos = [NSMutableArray array];
    for (int i = 0 ; i < self.arrModels.count; i++) {
        LZRestaurantPhotoModel *model = self.arrModels[i];
        //传递数据给浏览器
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,model.maxPhoto]];
        photo.srcImageView = (UIImageView*)recognizer.view; //设置来源哪一个UIImageView
        [photos addObject:photo];
    }
    brower.photos = photos;
    
    //3.设置默认显示的图片索引
    brower.currentPhotoIndex = recognizer.view.tag;
    
    //4.显示浏览器
    [brower show];
}


+(CGFloat)totleHeight:(NSArray*)urls {
    
    int rowNum = 4; // 每行的图片的最大数
    
    float width = 81*CGRectGetWidth([UIScreen mainScreen].bounds)/375.0;
    
    float padding = (CGRectGetWidth([UIScreen mainScreen].bounds)-width*rowNum-10*2)/3.0;
    NSInteger row = (urls.count-1)/rowNum+1;
    
    CGFloat imgTotleHeight = 40+row*width+(row-1)*padding;
    return 40+(urls.count>0?imgTotleHeight:0)+18;
}


@end
